﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Model;
using Moon.Common.WebInfo;
using Moon.Common;
using System.IO;
using GPI.Server;
using Server;

public partial class Admin_ContentManage_ImageAndDefault : System.Web.UI.Page
{
    //获得当前页面NodeId
    private string nodeid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            nodeid = this.Request.QueryString["id"] != null ? this.Request.QueryString["id"].ToString() : "";
            this.lbnAddContentInfo.CommandArgument = nodeid;
            this.lbnDefaultAdd.CommandArgument = nodeid;
            NodeInfo model = new NodeService().GetModel(nodeid.ToInt(0));
            if (model!=null&&model.Url.IsNullOrEmpty())
            {
                model.Url = WebConfigInfo.ShowAction + model.Id;
                new NodeService().Update(model);
               
            }
            this.labTitle.Text = model != null?model.NodeName:"";
            BindList(nodeid.ToInt(0));
        }
    }

    /// <summary>
    /// 保存或更新文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        ContentInfo model = new ContentInfo();
        model.contentTxt = this.txtContent.Text;
        //是否存在说明文件
        if (this.hfFile.Value.IsNullOrEmpty())
            model.contentFile = this.fuFile.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.FilePath + Guid.NewGuid().ToString() + this.fuFile.FileName.GetFileExt();
        else
            model.contentFile = this.hfFile.Value;

        //是否存在简图
        if (this.hfImage.Value.IsNullOrEmpty())
            model.contentImage = this.fuImage.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImage.FileName.GetFileExt();
        else
            model.contentImage = this.hfImage.Value;

        //是否存在大图
        if (this.hfBigImage.Value.IsNullOrEmpty())
            model.contentBigImage = this.fuBigImage.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuBigImage.FileName.GetFileExt();
        else
            model.contentBigImage = this.hfBigImage.Value;

        model.NodeID = this.lbnAddContentInfo.CommandArgument.ToInt(0);
        model.id = this.btnSaveTemplate.CommandArgument.ToInt(0);
        model.UpdateDate = DateTime.Now;
        bool isfuBigImage = upFile(this.fuBigImage, model.contentBigImage, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        bool isfuImage = upFile(this.fuImage, model.contentImage, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        bool isfuFile = upFile(this.fuFile, model.contentFile, this.Application["WebPath"].ToString(), true, WebConfigInfo.Fileformat);
        bool isTrue = false;
        if (!isfuBigImage || !isfuImage || !isfuFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + model.contentBigImage);
            CoFileHepler.DeleteFile(this.Application["WebPath"] + model.contentImage);
            CoFileHepler.DeleteFile(this.Application["WebPath"] + model.contentFile);
        }
        else
        {

            if (model.id == 0)
            {
                GPI.Server.ContentService ContentService = new GPI.Server.ContentService();
                isTrue = ContentService.Add(model);
            }
            else
            {
                GPI.Server.ContentService ContentService = new GPI.Server.ContentService();
                isTrue = ContentService.Update(model);
            }
        }

        if (isTrue)
            MessageBox.Show(this.Page, "操作成功！");
        else
            MessageBox.Show(this.Page, "操作失敗！");
        BindList(model.NodeID.Value);
    }

    /// <summary>
    /// 绑定文件列表
    /// </summary>
    /// <param name="id"></param>
    private void BindList(int id)
    {
        GPI.Server.ContentService server = new GPI.Server.ContentService();
        //获得总数
        int count = server.GetRecordCount(id);


        //切换至列表页
        this.MultiView1.ActiveViewIndex = 0;
        //设置每页信息数
        this.anpFileList.PageSize = 10;
        //信息总数
        this.anpFileList.RecordCount = count;
        //当前索引数
        int _CurrentPageIndex = this.anpFileList.CurrentPageIndex;
        //页面信息数
        int _PageSize = this.anpFileList.PageSize;
        //开始标识
        int starIndex = (_CurrentPageIndex - 1) * _PageSize <= 0 ? 0 : ((_CurrentPageIndex - 1) * _PageSize) + 1;
        // 结束标识
        int endIndex = _CurrentPageIndex * _PageSize;
        //根据标识搜索绑定数据表
        string[] orderstr = { "ID", "DESC" };
        this.repFileList.DataSource = server.GetListByPage(" and NodeID=" + id, orderstr, starIndex, endIndex);
        this.repFileList.DataBind();

        this.hfImage.Value = "";
        this.hfFile.Value = "";
        this.hfBigImage.Value = "";
        this.txtContent.Text = "";
    }
    /// <summary>
    /// 列表动作
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repFileList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        GPI.Server.ContentService server = new GPI.Server.ContentService();
        ContentInfo model = server.GetModel(e.CommandArgument.ObjectToInt(0));
        switch (e.CommandName)
        {
            case "edit":

                this.hfBigImage.Value = model.contentBigImage;
                this.hfFile.Value = model.contentFile;
                this.hfImage.Value = model.contentImage;
                this.txtContent.Text = model.contentTxt;
                this.btnSaveTemplate.CommandArgument = model.id.ToString();
                this.MultiView1.ActiveViewIndex = 1;
                break;
            case "delete":

                if (server.Delete(e.CommandArgument.ObjectToInt(0)))
                {
                    CoFileHepler.DeleteFile(this.Application["WebPath"] + model.contentBigImage);
                    CoFileHepler.DeleteFile(this.Application["WebPath"] + model.contentImage);
                    CoFileHepler.DeleteFile(this.Application["WebPath"] + model.contentFile);
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                BindList(this.lbnAddContentInfo.CommandArgument.ToInt(0));
                break;
        }
    }
    /// <summary>
    /// 列表分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void anpFileList_PageChanged(object sender, EventArgs e)
    {
        BindList(nodeid.ToInt(0));
    }


    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool upFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, bool isNULL, string CheckType)
    {
        //判断上传文件是否是空
        if (!Fupload.HasFile && isNULL)
        {
            return true;
        }
        //判断目录是否存在不存在则创建
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        //删除久的文件
        if (new FileInfo(dir + SaveName).Exists)
        {
            CoFileHepler.DeleteFile(dir + SaveName);
        }
        try
        {
            //獲得文件類型
            string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
            //判斷文件類型是否為需要的類型
            if (CoFileHepler.CheckValidExt(type, CheckType))
            {
                Fupload.PostedFile.SaveAs(dir + SaveName);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    protected void lbnAddContentInfo_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
    }
    #region 首页展示模块
    /// <summary>
    /// 信息表动作
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repDictionaryList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    /// <summary>
    /// 保存信息条
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveClass_Click(object sender, EventArgs e)
    {
        DictionaryService server = new DictionaryService();
        DictionaryInfo model = new DictionaryInfo();
        model.id = this.btnSaveClass.CommandArgument.ToInt(0);
        model.Nodeid = this.lbnDefaultAdd.CommandArgument.ToInt(0);
        model.Name = this.txtClassName.Text;
        model.Type = this.ddlType.SelectedValue;
        model.ParentID = 0;
        //如果存在封面
        if (fuImagePath.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.labImagePath.Text);
            model.ImagePath = this.fuImagePath.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImagePath.FileName.GetFileExt();
            upFile(fuImagePath, model.ImagePath, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        }
        else
        {
            model.ImagePath = labImagePath.Text;
        }
        //如果存在附件
        if (this.fuDictionaryFile.Visible && this.fuFile.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.txtValues.Text);
            model.Values = this.fuDictionaryFile.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.FilePath + Guid.NewGuid().ToString() + this.fuDictionaryFile.FileName.GetFileExt();
            upFile(fuDictionaryFile, model.Values, this.Application["WebPath"].ToString(), true, WebConfigInfo.Fileformat);
        }
        else
        { model.Values = this.txtValues.Text; }
        bool isAdd = false;
        if ("0".Equals(model.id.ToString()))
        {
            isAdd = server.Add(model);
        }
        else
        {
            isAdd = server.Update(model);
        }
        if (isAdd)
        {
            MessageBox.Show(this.Page, "操作成功！");
        }
        else
        {
            MessageBox.Show(this.Page, "操作失敗！");
        }
        this.MultiView1.ActiveViewIndex = 2;
    }
    /// <summary>
    /// 首页模块切换
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 选择信息类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {

        seleteTag();
    }
    /// <summary>
    /// 变化类型
    /// </summary>
    private void seleteTag()
    {
        switch (this.ddlType.SelectedValue)
        {
            case "http":
                this.txtValues.Visible = true;
                this.fuFile.Visible = false;
                break;
            case "file":
                this.fuFile.Visible = true;
                this.txtValues.Visible = false;
                break;
            default:
                this.txtValues.Visible = true;
                this.fuFile.Visible = false;
                break;
        }
    }


    #endregion

}
